package com.knife.poi.concurrent;

import org.apache.commons.lang3.concurrent.BasicThreadFactory;

import java.util.Objects;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * @author tianhao.luo@hand-china.com
 * @version 1.0
 * @date 2022年05月12日 星期四 7:47 下午
 * @description
 * @since 1.0
 */
public class ExportThreadPool {

    public static ThreadPoolExecutor th;


    public static ThreadPoolExecutor getThreadPool() {
        if (Objects.isNull(th)) {
            synchronized (ExportThreadPool.class) {
                if (Objects.isNull(th)) {
                    BasicThreadFactory.Builder builder = new BasicThreadFactory.Builder();
                    builder.namingPattern("ExportThreadPool-%d");
                    BasicThreadFactory threadFactory = builder.build();
                    th = new ThreadPoolExecutor(2,
                            16,
                            30,
                            TimeUnit.SECONDS,
                            new SynchronousQueue<>(),
                            threadFactory,
                            new ThreadPoolExecutor.CallerRunsPolicy()
                    );
                }
            }
        }
        return th;
    }
}
